package com.javaranger.news;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.StatusLine;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import com.javaranger.http.HttpClientUtils;

public class BanchUtils {

	public static void main(String[] args) {
		HttpClientUtils httpClientUtils  = new HttpClientUtils();
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		//fnid=HFII6eBm40&u=alibaba&p=0505011002
		params.add(new BasicNameValuePair("fnid", "test"));
		params.add(new BasicNameValuePair("u", "beyond"));
		params.add(new BasicNameValuePair("p", ""));
		HttpResponse response  = httpClientUtils.post("http://news.dbanotes.net/y", params);
		
		StatusLine status = response.getStatusLine();
		
		HttpEntity entity = response.getEntity();
		if (status != null && status.getStatusCode() == HttpStatus.SC_OK) {
			try {
				EntityUtils.toString(entity);
			} catch (ParseException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (status.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY
				|| status.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
			// 从头中取出转向的地址
			Header locationHeader = response.getFirstHeader("location");
			String location = null;
			if (locationHeader != null) {
				location = locationHeader.getValue();
				System.out.println("The page was redirected to:" + location);
			} else {
				System.err.println("Location field value is null.");
			}
		}
	}

}
